//========encoding:utf-8=========
/*
 * @describe:  三目运算符
 * @author:mhao
 * @copyright:chinasoft.cn
 * @date:2020年2月15日   上午11:52:00
 */
package edu.chinasoft.util;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

public class PropertiesTool {
	//把数据写入到属性文件里
	public static void writerProperties(int number,String path) {
		//创建属性文件类对象
		Properties properties = new Properties();
		//把需要写入的内容保存到属性文件类对象里
		properties.setProperty("visitnumber", number+"");
		//把内容写入到属性文件里
		FileWriter fileWriter=null;
		try {
			//io  以字符为单位的输出流
			fileWriter=new FileWriter(path);
			properties.store(fileWriter, null);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				fileWriter.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	//从属性文件里读取数据
	public static int readProperties(String path) {
		int result=0;
		//创建一个属性文件类对象
		Properties properties = new Properties();
		//把属性文件类对象关联到属性文件上
		FileReader fileReader=null;
		try {
			fileReader = new FileReader(path);
			properties.load(fileReader);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				fileReader.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		//通过方法获取key值对应的value
		result=Integer.parseInt((String)properties.get("visitnumber"));
		return result;
	}
}
